package com.overlord.im.connector.message.resolver;

import com.google.common.collect.Lists;
import com.overlord.im.commons.message.OverlordImMessage;
import com.overlord.im.connector.message.service.SendMessageService;
import com.overlord.im.connector.mq.sender.AckMessageSenderRabbitMqSender;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName MessageResolverFactory
 * @Author wuwei
 * @Description NONE
 * @Date 2020/6/2 14:20
 **/
@Service
public class MessageResolverFactory implements InitializingBean {

    @Resource
    private SendMessageService sendMessageService;

    @Resource
    private AckMessageSenderRabbitMqSender ackMessageSenderRabbitMqSender;
    private static final List<Resolver> RESOLVERS = Lists.newArrayList();

    public static void registerResolver(Resolver resolver) {
        RESOLVERS.add(resolver);
    }

    public static Resolver resolver(OverlordImMessage message) {
        for (Resolver resolver : RESOLVERS) {
            if (resolver.support(message)) {
                return resolver;
            }
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        MessageResolverFactory.registerResolver(new SimpleMessageResolver(sendMessageService));
        MessageResolverFactory.registerResolver(new HeartBeatMessageResolver());
//        MessageResolverFactory.registerResolver(new MessageAckResolver(ackMessageSenderRabbitMqSender));
    }
}
